
import { ipcApiRoute } from '@/api/main';
import { ipc } from '@/utils/ipcRenderer';
/**
 * 自定义 组件props传值对比
 * @param oldProps
 * @param newProps
 * @returns boolean
 */
export const arePropsEqual = (oldProps: any, newProps: any): boolean => {
  if (JSON.stringify(oldProps) === JSON.stringify(newProps)) {
    return true;
  }
  return false;
};

/**
 * 处理svg图片字符串
 * @param svgString
 * @returns
 */
export const parseSVG = (svgString: string) => {
  const parser = new DOMParser();

  const xmlDoc = parser.parseFromString(svgString, 'image/svg+xml');

  return xmlDoc.documentElement.outerHTML;
};

/**
 * 删除tab内信息，页面跳转逻辑
 * @param count
 * @param queryParams
 * @param setQueryParams
 */
export const pageDelJump = (count: number, ids: string, queryParams: { pageNum: number; pageSize: number }, setQueryParams: any) => {
  const pageNum = Math.ceil((count - ids.split(',').length) / queryParams.pageSize);
  if (pageNum < queryParams.pageNum) {
    setQueryParams({
      pageNum: pageNum || 1,
      pageSize: queryParams.pageSize,
    });
  } else {
    setQueryParams({
      pageNum: queryParams.pageNum,
      pageSize: queryParams.pageSize,
    });
  }
};


export  const  downloadJSON = async (data: object, filename: string) => {
      await ipc.invoke(ipcApiRoute.updateLenoAdminJSON, {leno_admin:data})
      return
    // const params = {
    //   action: 'getDataDir',
    // }
    // let {result} = await ipc.invoke(ipcApiRoute.jsondbOperation, params)
      // console.log(`result`,result)
      // return
    // 创建 JSON 字符串
    const jsonStr = JSON.stringify(data,null,2);
    
    // 创建 Blob 对象
    const blob = new Blob([jsonStr], { type: 'application/json' });
  
    // 创建一个 URL 对象
    const url = URL.createObjectURL(blob);
  
    // 创建一个链接
    const link = document.createElement('a');
    link.href = url;
    link.download = filename; // 设置下载文件的名称
  
    // 模拟点击链接进行下载
    document.body.appendChild(link);
    link.click();
  
    // 清理资源
    setTimeout(() => {
      URL.revokeObjectURL(url);
      document.body.removeChild(link);
    }, 0);
  }